// pages/goods/components/goodsnav/index.js

var app = getApp();
var core = app.requirejs('core');
var mainui = app.requirejs('mainui');
const api = require("../../../../utils/api.js")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goods: {
      type: Object
    },
    optionId: {
      type: Number
    },
    specsData: {
      type: Array
    },
    cartcount: {
      type: Number
    },
    total: {
      type: Number
    },
    showGiftStatus: {
      type: Boolean
    }
  },
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的初始数据
   */
  data: {
    mainuiToast: {
      show: false,
      text: ''
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 立即购买
    buyNow: function (event) {
      let $this = this
      let memberId = app.getCache('userinfo_id');
      // wx.navigateTo({
      //   url: '/pages/order/create/index?optionId=' + $this.data.optionId + '&goodsId=' + $this.data.goods.id + '&total=1&memberId=' + memberId
      // });
      if ($this.data.specsData.length > 0) {
        wx.navigateTo({
          url: '/pages/order/create/index?optionId=' + $this.data.optionId + "&type=1&total=" + $this.data.total + '&goodsId=' + $this.data.goods.id + "&typeOption=" + $this.data.specsData[0].type + '&typeword=' + $this.data.specsData[0].typeword + "&ordertype=" + $this.data.ordertype + "&goodstype=" + $this.data.goods.type
        });
      } else {
        wx.navigateTo({
          url: '/pages/order/create/index?optionId=' + $this.data.optionId + "&type=1&total=" + $this.data.total + '&goodsId=' + $this.data.goods.id + "&ordertype=" + $this.data.ordertype + "&goodstype=" + $this.data.goods.type
        });
      }
      // goodspicker.buyNow(event, $this, "goods_detail")
    },
    //加入购物车
    getCart: function (event) {
      var $this = this
      let properties = $this.properties
      console.log(properties)
      let apicontent = api.orders.addCard()
      let memberId = app.getCache('userinfo_id');
      var optionId = properties.optionId;
      let data = {
        total: $this.data.total,
        optionId: optionId,
        goodsId: properties.goods.id,
        typeOption: properties.specsData.length > 0 ? properties.specsData[0].type : '',
        typeword: properties.specsData.length > 0 ? properties.specsData[0].typeword : '',
      }
      core.post(apicontent, {
        ...data
      }, function (ret) {
        if (ret.code == 1) {
          wx.showToast({
            icon: 'success',
            title: '添加购物车成功',
          });
          $this.getcartGoodsCount()
        } else {
          console.log(properties.goods.specsData)
          // mainui.toast($this, "库存不足");
          mainui.toast($this, ret.message);
        }
      })
      app.sr.track('add_to_cart', {
        action_type: 'append_to_cart',
        sku_num: 1,
        "sku": {
          "sku_id": String(properties.specsData[0].id),
          "sku_name": properties.specsData[0].title
        },
        "spu": {
          "spu_id": String(properties.goods.id),
          "spu_name": properties.goods.title
        },
      })

    },
    // 购买picker
    selectPicker: function (e) {
      console.log(e)
      let $this = this;
      var status = e.currentTarget.dataset.status; //商品状态是否可售
      if (status != 1) {
        console.log(1)
        mainui.toast($this, '库存不足');
        return false
      }
      if (e.target.dataset.buytype) {
        this.setData({
          buyType: e.target.dataset.buytype,
          ordertype: e.currentTarget.dataset.ordertype
        })
      }
      let mobile = app.getCache('mobile');
      // buytype为购物车购买  加入购物车
      if (this.data.buyType == 'cart') {
        //绑定节点为加入购物车节点
        //加入购物车绑定节点
        // if ($this.data.bindingNodes == 4 && mobile.length < 1) {
        //   core.alert("请绑定手机", function() {

        //     wx.navigateTo({
        //       url: '/pages/member/bind/index',
        //     })
        //   })
        //   return
        // }
        this.getCart($this);
      }
      //立即购买
      if (this.data.buyType == 'buy') {
        // 绑定节点为立即购买节点
        if (this.data.bindingNodes == 2 && mobile.length < 1) {
          core.alert("请绑定手机", function () {

            wx.navigateTo({
              url: '/pages/member/bind/index',
            })
          })
          return
        }
        this.buyNow($this);
      }
      // 定制服务
      if (this.data.buyType == 'dingzhi') {
        $this.makeServer(e);
      }
      // if (this.data.canBuy != '' && this.data.buyType != 'dingzhi') {
      //   mainui.toast($this, '库存不足');
      // }
    },

    //定制服务 刻字服务
    makeServer: function (e) {
      var $this = this;
      var id = e.target.dataset.id;
      let optionId = this.data.optionId
      if ($this.data.goods.letterStatus == 1) {
        // 刻字服务为1 跳到第一步
        wx.redirectTo({
          url: '../../customization/step1/index?goodsId=' + id + '&optionId=' + optionId
        })
      } else if ($this.data.goods.choosekapian == 1) {
        // cardStatus 专属卡片 跳到第二步

        wx.redirectTo({
          url: '../../customization/step2/index?goodsId=' + id + '&optionId=' + optionId
        })
      } else if ($this.data.goods.packetStatus == 1) {
        // packetStatus 个性套装 跳到第三步
        wx.redirectTo({
          url: '../../customization/step3/index?goodsId=' + id + '&optionId=' + optionId
        })
      }
      // }
    },
    //获取购物车的总数
    getcartGoodsCount() {
      let apicontent = api.orders.cartGoodsCount()
      let memberId = app.getCache('userinfo_id');
      let $this = this
      core.post(apicontent, {
        memberId
      }, function (res) {
        if (res.data > 0) {
          $this.setData({
            cartcount: res.data,
          })
        }
      })
    },
    // 送礼
    sendgift: function (event) {
      let goods = this.data.goods,
        $this = this
      if (goods.status == 1) {
        console.log(event)
        let $this = this;
        console.log($this)
        wx.navigateTo({
          url: '/add/sendGift/index?&type=4&total=' + $this.data.total + '&goodsId=' + $this.data.goods.id + '&optionId=' + $this.data.optionId
        })
      } else {
        foxui.toast($this, "库存不足");
      }
    },
  }
})